//
//  HomeEqualizerTableViewCell.swift
//  EasePods
//
//  Created by 易先生 on 2024/4/8.
//

import UIKit
import RxSwift
import RxCocoa
import CRPEarPhone

class HomeEqualizerTableViewCell: TableViewCell {
    
    override func makeUI() {
        super.makeUI()

        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        self.contentView.addSubview(titleLabel)
        self.contentView.addSubview(collectionView)
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
        }
        
        let height = (UIScreen.main.bounds.width-68)/4
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.leading.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(height)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
    
    func bind(viewModel: HomeEqualizerCellViewModel) {
        
        viewModel.equalizerItems.bind(to: collectionView.rx.items(cellIdentifier: HomeEqualizerCollectionCell.reuseIdentifier, cellType: HomeEqualizerCollectionCell.self)) { row, item, cell in
            cell.bind(to: item.item, isSelect: item.isSelect)
        }.disposed(by: cellDisposeBag)
        
        collectionView.rx.itemSelected.subscribe(onNext: { indexPath in
            let item = viewModel.equalizerItems.value[indexPath.row]
            viewModel.selectItme.accept(item.item)
        }).disposed(by: cellDisposeBag)
        
    }
    
    private lazy var titleLabel: Label = {
        let lable = Label()
        lable.font = Fonts.systemFont(ofSize: 16)
        lable.text = R.string.localizable.home_equalizer_section_title()
        lable.textColor = R.color.common_3_text()
        return lable
    }()
    
    private lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayoutView)
        collectionView.backgroundColor = .clear
        collectionView.register(HomeEqualizerCollectionCell.self, forCellWithReuseIdentifier: HomeEqualizerCollectionCell.reuseIdentifier)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    private lazy var flowLayoutView: UICollectionViewFlowLayout = {
        
        let width = (UIScreen.main.bounds.width-68)/4
        let height = width
        let flowLayoutView = UICollectionViewFlowLayout()
        flowLayoutView.scrollDirection = .horizontal
        flowLayoutView.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        flowLayoutView.minimumInteritemSpacing = 0
        flowLayoutView.minimumLineSpacing = 12
        flowLayoutView.itemSize = CGSize(width: width - 5 , height: height)
        return flowLayoutView
    }()

}

class HomeEqualizerCollectionCell: CollectionViewCell {
    
    override func makeUI() {
        
        self.contentView.addSubview(cardView)
        cardView.addSubview(stackView)
        
        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        stackView.addArrangedSubview(iconImageView)
        stackView.addArrangedSubview(titleLabel)
        stackView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        iconImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
    }
    
    func bind(to item: CRPEQType, isSelect: Bool) {
        titleLabel.text = item.title
        if isSelect {
            cardView.backgroundColor = R.color.equalizer_2_chosen()
            titleLabel.textColor = R.color.equalizer_3_chosen_text()
            iconImageView.image = item.selectIconImage
        } else {
            cardView.backgroundColor = R.color.equalizer_4_no_chosen()
            titleLabel.textColor = R.color.equalizer_5_no_chosen_text()
            iconImageView.image = item.iconImage
        }
    }
    
    private lazy var cardView: View = {
        let view = View()
        view.backgroundColor = R.color.equalizer_4_no_chosen()
        view.layer.cornerRadius = 12
        view.layer.masksToBounds = true
        return view
    }()
    
    
    lazy var stackView: StackView = {
        let view = StackView()
        view.alignment = .center
        view.axis = .vertical
        view.spacing = 8
        return view
    }()
    
    private lazy var iconImageView: ImageView = {
        let imageView = ImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    private lazy var titleLabel: Label = {
        let lable = Label()
        lable.font = Fonts.mediumSystemFont(ofSize: 12)
        lable.textColor = R.color.equalizer_5_no_chosen_text()
        return lable
    }()
}

extension CRPEQType {
    
    var title: String? {
        switch self {
        case .normal:
            return R.string.localizable.home_equalizer_standard_title()
        case .rock:
            return R.string.localizable.home_equalizer_rock_title()
        case .pop:
            return R.string.localizable.home_equalizer_pop_title()
        case .jazz:
            return R.string.localizable.home_equalizer_jazz_title()
        case .country:
            return R.string.localizable.home_equalizer_country_title()
        case .classic:
            return R.string.localizable.home_equalizer_classic_title()
        case .custom:
            return nil
        default:
            return nil
        }
    }
    
    var iconImage: UIImage? {
        switch self {
        case .normal:
            return R.image.ic_equalizer_1_n()
        case .rock:
            return R.image.ic_equalizer_2_n()
        case .pop:
            return R.image.ic_equalizer_3_n()
        case .jazz:
            return R.image.ic_equalizer_4_n()
        case .country:
            return R.image.ic_equalizer_5_n()
        case .classic:
            return R.image.ic_equalizer_6_n()
        case .custom:
            return nil
        default:
            return nil
        }
    }
    
    var selectIconImage: UIImage? {
        switch self {
        case .normal:
            return R.image.ic_equalizer_1_h()
        case .rock:
            return R.image.ic_equalizer_2_h()
        case .pop:
            return R.image.ic_equalizer_3_h()
        case .jazz:
            return R.image.ic_equalizer_4_h()
        case .country:
            return R.image.ic_equalizer_5_h()
        case .classic:
            return R.image.ic_equalizer_6_h()
        case .custom:
            return nil
        default:
            return nil
        }
    }
}
